package com.yupi.yupicturebackend.exception;

/**
 * 为了更方便地根据情况抛出异常，封装一个ThrowUtils类，类似断言，简化抛异常的代码
 */
public class ThrowUtils {

    /**
     * 条件成立则抛出异常
     * @param condition         条件
     * @param runtimeException  异常
     */
    public static void throwIf(boolean condition, RuntimeException runtimeException) {
        if(condition) {
            throw runtimeException;
        }
    }

    /**
     * 条件成立则抛出异常
     * @param condition 条件
     * @param errorCode 错误码
     */
    public static void throwIf(boolean condition, ErrorCode errorCode) {
        throwIf(condition, new BusinessException(errorCode));
    }

    /**
     * 条件成立则抛出异常
     * @param condition 条件
     * @param errorCode 错误码
     * @param message   错误信息
     */
    public static void throwIf(boolean condition, ErrorCode errorCode, String message) {
        throwIf(condition, new BusinessException(errorCode, message));
    }
}
